package com.galaxy.commom.base.exception;

import com.galaxy.commom.base.enums.ResultCodeEnum;

/**
 * 基础异常类
 * @author caijinbang
 * @date 2018/9/7 20:23
 */
public abstract class BaseException extends RuntimeException {

  private ResultCodeEnum errCode;
  private String errMsg;

  public BaseException() {
  }

  public BaseException(Throwable ex) {
    super(ex);
  }

  public BaseException(String message) {
    super(message);
  }

  public BaseException(String message, Throwable ex) {
    super(message, ex);
  }




  public BaseException(ResultCodeEnum errCode, String... errMsg) {
    super(errCode.getMessage());
    this.errCode = errCode;
    setErrMsg(errMsg,true);
  }

  public BaseException(ResultCodeEnum errCode, String errMsg,Boolean isTransfer) {
    super(errMsg);
    this.errCode = errCode;
    setErrMsg(new String[]{errMsg},isTransfer);
  }

  /**
   * 构造函数
   *
   * @param cause 异常
   */
  public BaseException(ResultCodeEnum errCode, Throwable cause, String... errMsg) {
    super(errCode.getValue() + errCode.getMessage(), cause);
    this.errCode = errCode;
    setErrMsg(errMsg,true);
  }

  public ResultCodeEnum getErrCode() {
    return errCode;
  }

  public void setErrCode(ResultCodeEnum errCode) {
    this.errCode = errCode;
  }

  public String getErrMsg() {
    return this.errMsg;
  }

  public void setErrMsg(String[] errMsg,Boolean isTransfer) {

    if (null != errMsg &&errMsg.length>0) {
      if(errCode.getMessage().contains("%s") && isTransfer){
        this.errMsg = String.format(errCode.getMessage(), errMsg);
      }else{
        StringBuffer sf = new StringBuffer();
        for (String msg : errMsg) {
          sf.append(msg+";");
        }
        this.errMsg = sf.toString();
      }
    }else{
      this.errMsg = errCode.getMessage();
    }

  }
}
